package com.book.controller;

import com.book.entity.Book;
import com.book.entity.BookAndStudent;
import com.book.entity.Category;
import com.book.entity.Student;
import com.book.model.BaseImg;
import com.book.service.BookAndStudentService;
import com.book.service.BookInfoService;
import com.book.service.CategoryInfoService;
import com.book.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by 落叶的思恋 on 2017/6/9.
 */
@RestController
@RequestMapping("/Book")
public class BookController {
    @Autowired
    CategoryInfoService categoryInfoService;
    @Autowired
    BookInfoService bookInfoService;
    @Autowired
    BookAndStudentService bookAndStudentService;
    @Autowired
    StudentInfoService studentInfoService;

    @RequestMapping(value = "/Category", method = RequestMethod.GET)
    public List<Category> selectCate() {
        List<Category> list = null;
        if (categoryInfoService.select() != null) {
            list = categoryInfoService.select();
        } else {
            return null;
        }
        return list;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public int insertCate(@RequestBody Book book, HttpServletRequest request) throws IOException {
        String path = request.getSession().getServletContext().getRealPath("/resources/img");
        System.out.println(book.getBookimg());

        book.setBookimg(new BaseImg().GenerateImage(book.getBookimg(), path));
        int n = bookInfoService.insert(book);
        if (n != 0) {

            System.out.println("上传成功" + book.getBookimg() + "数目");
            return n;
        } else {
            return 0;
        }

    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)

    public List<Book> selectBook() {
        List<Book> list = null;
        list = bookInfoService.select();
        if (list != null) {
            return list;
        } else {
            return null;
        }

    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public int DeleteBook(@RequestBody Book book) {
        int n = bookInfoService.delete(book.getBookId());
        if (n != 0) {
            return n;
        } else {
            return 0;
        }

    }

    /**
     * 数据
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public int UpdateBook(@RequestBody Book book) {
        int n = bookInfoService.updateByPrimaryKey(book);
        System.out.println(n);
        if (n != 0) {
            return n;
        } else {
            return 0;
        }

    }

    /**
     * @param request
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public List<Book> searchBook(HttpServletRequest request) {
        String search = request.getParameter("search");
        System.out.println("将要搜索的值" + search);
        List<Book> book = bookInfoService.selectsearch(search);
        return book;
    }

    @RequestMapping(value = "/borrowbook", method = RequestMethod.POST)
    public int borrowBook(@RequestBody BookAndStudent bookAndStudent, HttpServletRequest request) {
        Book book = bookInfoService.selectByPrimaryKey(bookAndStudent.getBookId());
        book.setBookNum(book.getBorrownum() + 1);
        Student student = studentInfoService.selectByPrimaryKey(bookAndStudent.getStudentId());
        student.setBorrownum(student.getBorrownum() + 1);
        System.out.println("收到的时间是" + bookAndStudent.getNowtime());
        int n = bookAndStudentService.insert(bookAndStudent);
        if (n != 0) {

            book.setBookId(bookAndStudent.getBookId());
            bookInfoService.updateByPrimaryKey(book);
            studentInfoService.updateByPrimaryKey(student);
        }
        return n;
    }
}
